from flask import Flask, request, jsonify, render_template
from flask_mysqldb import MySQL
import requests

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


# 配置 MySQL 连接参数
app.config['MYSQL_HOST'] = 'localhost'  # 或者你的实际数据库主机名
app.config['MYSQL_PORT'] = 3306
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'wanglibo123..'
app.config['MYSQL_DB'] = 'Song'
mysql = MySQL(app)


@app.route('/search', methods=['GET'])
def search():
    query = request.args.get('query', '')
    cursor = mysql.connection.cursor()
    try:
        cursor.execute(
            "SELECT songname, singer, albumname, download_link FROM Songs WHERE songname LIKE %s OR singer LIKE %s OR albumname LIKE %s",
            ('%' + query + '%', '%' + query + '%', '%' + query + '%'))
        songs = cursor.fetchall()
    finally:
        cursor.close()
    return render_template('search_results.html', songs=songs)  # 使用新的模板


if __name__ == '__main__':
    app.run(debug=True)
